package ssi.servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ssi.tools.Connector;

/**
 * Servlet implementation class ActivationServlet
 */
@WebServlet("/ActivationServlet")
public class ActivationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ActivationServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void doGet(HttpServletRequest request,
		HttpServletResponse response) throws ServletException, IOException {
	
		request.setCharacterEncoding("UTF-8");
		String hashCode = request.getParameter("code");
		try {
			Connection connection = new Connector().getConnection();
			Statement statement = connection.createStatement();
			ResultSet result = statement.executeQuery("SELECT id FROM `users` WHERE `activation`=\""+ hashCode + "\"");
			if (result.next()) {
				int id = result.getInt(1);
				statement.executeUpdate("UPDATE `users` SET `activation`='' WHERE id="+ id);
				response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
				response.setHeader("Location", "/SSIProject/index.jsp?page=activationSuccess");
				result.close();
				connection.close();
			} else {
			
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
		response.setHeader("Location", "/SSIProject/index.jsp?page=activationSuccess");

	}

}
